using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Web;
using System.Web.Caching;
using System.Web.SessionState;
using TestSystem.BLL.TestSystem;

namespace TestSystem.BLL
{
    public abstract class BusinessEntity
    {
        private static int maxRows = int.MaxValue;
        protected static int MaxRows
        {
            get { return maxRows; }
        }

        public BusinessEntity()
        {
        }

        protected BusinessEntity(IDataReader rdr)
        {
            this.Load(rdr);
        }

        protected abstract void Load(IDataReader rdr);

        protected static T LoadEntity<T>(IDataReader rdr) where T : BusinessEntity, new()
        {
            T res = default(T);
            if (rdr.Read())
            {
                res = new T();
                res.Load(rdr);
            }
            rdr.Close();
            return res;
        }

        protected static List<T> LoadList<T>(IDataReader rdr) where T : BusinessEntity, new()
        {
            List<T> res = new List<T>();
            while (rdr.Read())
            {
                T t = new T();
                t.Load(rdr);
                res.Add(t);
            }
            rdr.Close();
            return res;
        }

        protected static Dictionary<int, List<Answer>> LoadAnswerDictionary(IDataReader rdr)
        {
            Dictionary<int, List<Answer>> res = new Dictionary<int, List<Answer>>();
            while(rdr.Read())
            {
                Answer ans = new Answer();
                ans.Load(rdr);
                if(res.ContainsKey(ans.TestID))
                {
                    res[ans.TestID].Add(ans);
                }
                else
                {   
                    List<Answer> answers = new List<Answer>();
                    answers.Add(ans);
                    res.Add(ans.TestID, answers);
                }
            }
            return res;
        }

        public abstract void Update();

        public static Cache Cache
        {
            get
            {
                if(HttpContext.Current != null)
                    return HttpContext.Current.Cache;
                return null;
            }
        }

        public static HttpSessionState Session
        {
            get
            {
                if (HttpContext.Current != null)
                    return HttpContext.Current.Session;
                return null;
            }
        }
        
        protected static void RemoveCacheItems(string prefix)
        {
            prefix = prefix.ToLower();
            List<string> itemsToRemove = new List<string>();

            if (Cache != null)
            {
                IDictionaryEnumerator enumerator = Cache.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    if (enumerator.Key.ToString().ToLower().StartsWith(prefix))
                    {
                        itemsToRemove.Add(enumerator.Key.ToString());
                    }
                }
                foreach (string s in itemsToRemove)
                {
                    Cache.Remove(s);
                }
            }
        }

        protected static void CacheData(string key, object data, int cacheDuration, int slideDuration)
        {
            if (data != null && Cache != null)
            {
                if(slideDuration == -1)
                {
                    Cache.Insert(key, data, null, DateTime.Now.AddSeconds(cacheDuration), TimeSpan.Zero);
                }
                else if(cacheDuration == -1)
                {
                    Cache.Insert(key, data, null, DateTime.MaxValue, TimeSpan.FromSeconds(slideDuration));
                }
            }
        }

        protected static int PageIndex(int startIndex, int maxRows)
        {
            if (maxRows < 0)
                return 0;
            return (int)Math.Floor((double)startIndex / (double)maxRows);
        }

        protected static void DeleteImage(string path)
        {
            File.Delete(path);
        }
    }
}


